package com.box.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.box.entity.DbRoles;
import com.box.entity.DbRolesExample;
import com.box.entity.DbRolesExample.Criteria;
import com.box.entity.DbRolesMenusExample;
import com.box.entity.DbRolesMenusKey;
import com.box.mapper.DbRolesMapper;
import com.box.mapper.DbRolesMenusMapper;
import com.box.service.RolesService;
import com.box.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;







/**
 * @author Administrator
 *
 */
@Service
public class RolesServiceImpl implements RolesService {
	
	@Autowired
	private DbRolesMapper rolesMapper;
	
	@Autowired
	private DbRolesMenusMapper dbRolesMenuMapper;

	public LayuiResult selRoles(Integer page, Integer limit) {
		// TODO Auto-generated method stub
		try {
			PageHelper.startPage(page, limit);
			DbRolesExample example=new DbRolesExample ();
		List<DbRoles> list = rolesMapper.selectByExample(example);
		PageInfo<DbRoles> pageinfo=new PageInfo<DbRoles>(list);
		LayuiResult result=new LayuiResult();
		result.setCode(0);
		result.setCount(pageinfo.getTotal());
		result.setMsg("角色获取成功");
		result.setData(pageinfo.getList());
		return result;
		} catch (Exception e) {
			LayuiResult result=new LayuiResult();
			result.setCode(1);
			result.setCount(null);
			result.setMsg("角色获取失败");
			result.setData(e);
			return result;
		}
	
		

	}

	public LayuiResult delRolesById(Long roleId) {
		// TODO Auto-generated method stub
		try {
			int n= rolesMapper.deleteByPrimaryKey(roleId);
			
			return new LayuiResult(0, "单个删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, "单个删除失败");
		}
	
	}

	public LayuiResult delRolesByAll(String roleStr) {
		// TODO Auto-generated method stub
		Integer n=0;
		try {
			String[] ids = roleStr.split(",");
			if(ids!=null&&ids.length>0){
				for (String roleId : ids) {
				n=	rolesMapper.deleteByPrimaryKey(Long.parseLong(roleId));
				}
				System.out.println(n+"========批量删除成功=========");
			}
			
			
			return new LayuiResult(0, "批量删除"+n+"成功");
		} catch (Exception e) {
			// TODO: handle exception
			return new LayuiResult(500, "批量删除失败");
		}
	
	}

	public List<DbRoles> selRoles() {
		DbRolesExample example = new DbRolesExample();
	Criteria criteria = example.createCriteria();
		List<DbRoles> list =rolesMapper.selectByExample(example);
		return list;
	}

	public Integer updRole(DbRoles role, String m) {
		// 更新角色信息
		rolesMapper.updateByPrimaryKey(role);
		// 先删除角色所有权限
		DbRolesMenusExample example = new DbRolesMenusExample();
		 com.box.entity.DbRolesMenusExample.Criteria createCriteria = example.createCriteria();
		 createCriteria.andRoleIdEqualTo(role.getRoleId());
		dbRolesMenuMapper.deleteByExample(example);
		Integer n=0;
		// 更新权限信息
		if (m != null && m.length() != 0) {
			String[] array = m.split(",");  
			List<String> result = new ArrayList<String>();  
			boolean flag;  
			for(int i=0;i<array.length;i++){  
			    flag = false;  
			    for(int j=0;j<result.size();j++){  
			        if(array[i].equals(result.get(j))){  
			            flag = true;  
			            break;  
			        }  
			    }  
			    if(!flag){  
			        result.add(array[i]);  
			    }  
			}  
			// 重新赋予权限
			if (result != null && result.size() > 0) {
				for (int i = 0; i < result.size(); i++) {
					DbRolesMenusKey record = new DbRolesMenusKey();
					record.setMenuId(Long.parseLong(result.get(i)));
					record.setRoleId(role.getRoleId());
					// 维护角色—菜单表
				n=	dbRolesMenuMapper.insert(record);
				}
			}
		}
		return n;
	}

	public DbRoles selRoleByRoleName(String roleName) {
		DbRolesExample example = new DbRolesExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleNameEqualTo(roleName);
		List<DbRoles> roles = rolesMapper.selectByExample(example);
		if (roles != null && roles.size() > 0) {
			return roles.get(0);
		}
		return null;
	}

	public Integer insRole(DbRoles role, String m) {
		//维护角色表
		rolesMapper.insert(role);
		Integer n=0;
		// 维护角色-菜单表
		if (m != null && m.length() != 0) {
			String[] array = m.split(",");  
			List<String> result = new ArrayList<String>();  
			boolean flag;  
			for(int i=0;i<array.length;i++){  
			    flag = false;  
			    for(int j=0;j<result.size();j++){  
			        if(array[i].equals(result.get(j))){  
			            flag = true;  
			            break;  
			        }  
			    }  
			    if(!flag){  
			        result.add(array[i]);  
			    }  
			}  
			// 重新赋予权限
			if (result != null && result.size() > 0) {
				for (int i = 0; i < result.size(); i++) {
					DbRolesMenusKey record = new DbRolesMenusKey();
					record.setMenuId(Long.parseLong(result.get(i)));
					record.setRoleId(role.getRoleId());
					// 维护角色—菜单表
				n=	dbRolesMenuMapper.insert(record);
				}
			}
		}
		return n;
	}

}
